<?php
// +----------------------------------------------------------------------
// | CLTPHP.Pro [会员管理]
// +----------------------------------------------------------------------
// | Copyright (c) 2019-2029 https://www.cltphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://www.cltphp.com/detail-140.html )
// +----------------------------------------------------------------------
// | Author: chichu <1109305987@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\Settings;
use app\common\model\OrderReason as M;
use think\App;
use think\Exception;
use think\facade\Request;
use think\facade\Db;
use think\facade\View;

class OrderReason extends AdminBase
{
    protected $validate = 'OrderReason';

    //列表
    public function index(){

        if(Request::isAjax()) {
            //分页
            $page =input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):config('pageSize');
            $page = ['list_rows'=> $pageSize, 'page' => $page];

            $where = [];


            //排序
            $sort_order = input('sort_order') ? input('sort_order') : 'desc';
            $order = input('sort_order')?[input('sort_by')=>$sort_order]:['id'=>$sort_order];

            $list = M::where($where)
                ->order($order)
                ->paginate($page)
                ->toArray();

            return ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }else{
            $view = [

            ];
            return view('',$view);
        }
    }

    //字段值修改
    public function cf(){
        $data = input('post.');
        foreach ($data as $k=>$v){
            if(!in_array($k,['id','status','sort'])){
                unset($data[$k]);
            }
        }
        $result = M::editSave($data);
        Settings::saveOrderReason();
        return $result;
    }

    public function add(){
        if(request()->isPost()){
            $data = input('post.');
            $result = $this->validate($data, $this->validate);
            if (true !== $result) {
                // 验证失败 输出错误信息
                return $this->returnError($result);
            }
            $result = M::addSave($data);
            Settings::saveOrderReason();
            return $result;
        }else {
            $view = [
                'info' => '',
            ];
            return view('form', $view);
        }
    }
    public function edit(){
        if(request()->isPost()){
            $data = input('post.');
            $result = $this->validate($data, $this->validate);
            if (true !== $result) {
                // 验证失败 输出错误信息
                return $this->returnError($result);
            }
            $result = M::editSave($data);
            Settings::saveOrderReason();
            return $result;
        }else {
            $id = intval(input('param.id'));
            if (empty($id)) {
                die('缺少必要的参数！');
            }
            $info = M::where('id', $id)->find();

            $view = [
                'info' => $info,
            ];
            return view('form', $view);
        }
    }

    // 删除
    public function del($id){
        if($id){
            M::destroy($id);
            Settings::saveOrderReason();
        }
        return ['code'=>0,'msg'=>'删除成功!'];
    }


}